package com.hnjfzn.auth.factory;

import com.hnjfzn.auth.service.SysPasswordService;
import com.hnjfzn.auth.service.SysRecordLogService;
import com.hnjfzn.common.core.constant.Constants;
import com.hnjfzn.common.core.exception.ServiceException;
import com.hnjfzn.common.core.utils.StringUtils;
import com.hnjfzn.common.redis.service.RedisService;
import com.hnjfzn.system.api.RemoteUserService;
import com.hnjfzn.system.api.domain.SysUser;
import org.springframework.stereotype.Service;

@Service
public class SmsLogin extends AbsSysLogin {
    /**
     * 构造
     *
     * @param remoteUserService 远程用户服务
     * @param passwordService   密码服务
     * @param recordLogService  日志服务
     * @param redisService      缓存服务
     */
    public SmsLogin(RemoteUserService remoteUserService, SysPasswordService passwordService, SysRecordLogService recordLogService, RedisService redisService) {
        super(remoteUserService, passwordService, recordLogService, redisService);
    }

    @Override
    public void validAfter(String... args) {
        String phone = args[0];
        // 参数校验
        if (StringUtils.isAnyBlank(phone))
        {
            recordLogService.recordLogininfor(phone, Constants.LOGIN_FAIL, "手机号必须填写");
            throw new ServiceException("手机号必须填写");
        }
        // 手机号校验
        if (!phone.matches("^1[3-9]\\d{9}$")) {
            recordLogService.recordLogininfor(phone, Constants.LOGIN_FAIL, "请输入正确的手机号");
            throw new ServiceException("请输入正确的手机号");
        }
    }

    @Override
    public void validBefore(SysUser user, String... args) {
        // 手机号登录无后期验证，有需要在这里补充
    }
}
